﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace InputProductForm
{
    public partial class frmAddtype : UTLBASEFORM.MDICHILD
    {
        public frmAddtype()
        {
            InitializeComponent();
        }
        DataTable dataType = new DataTable();
        Info.InfoType info = new InputProductForm.Info.InfoType();
        TType tType = new TType();

        private void frmAddtype_Load(object sender, EventArgs e)
        {
            LoadType();
            ChangeButton("Insert");
        }
        private void SetupMenuBar()
        {
            ShowBTN1(TextLabelAdd, ImageNameCheck_Ok);
            ShowBTN2(TextLabelEdit, ImageNameEdit);
            ShowBTN3("Hủy", ImageNameCancel);

        }
        private void LoadType()
        {

            dataType = tType.GetType();
            lvProType.Items.Clear();
            string active;
            for (int i = 0; i < dataType.Rows.Count; i++)
            {
                int stt = i + 1;
                ListViewItem item = new ListViewItem(stt.ToString());
                lvProType.Items.Add(item);
                item.SubItems.Add(dataType.Rows[i]["Sym"].ToString());
                item.SubItems.Add(dataType.Rows[i]["Name"].ToString());
              
                if (dataType.Rows[i]["Active"].ToString() == "True")
                    active = "Hoạt động";
                else active = "Khóa";
                item.SubItems.Add(active);

                if (i % 2 == 0)
                {
                    item.BackColor = Color.AliceBlue;
                    //return ;
                }
                else
                {
                    item.BackColor = Color.AntiqueWhite;
                    //return Brushes.Beige;
                }

            }

        }
        public Info.InfoType SetInfo()
        {
            info.Name = txtName.Text;
            info.Sym = txt_sym.Text;
            if (cmbActive.Text.Trim() == "Khóa")
                info.Active = false;
            else info.Active = true;
            return info;
        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            SetInfo();
            tType.Insert(info);
            LoadType();
            RefeshForm();
        }

        private void cmdUpdate_Click(object sender, EventArgs e)
        {
            UpdateType();
            ChangeButton("Insert");
        }
        public void UpdateType()
        {
            SetInfo();
            tType.Update(info);
            LoadType();
            // RefeshForm();
        }
       
        private void cmdCancel_Click(object sender, EventArgs e)
        {
            ChangeButton("Insert");
        }
        private void ChangeButton(string state)
        {
            if (state == "Insert")
            {
                cmdAdd.Enabled = true;
                cmdUpdate.Enabled = false;
                RefeshForm();
            }
            else if (state == "Update")
            {

                cmdAdd.Enabled = false;
                cmdUpdate.Enabled = true;
            }
        }
        public void RefeshForm()
        {
            txtName.Text = "";
            txt_sym.Text = "";
        }
        public void ShowListToForm(int id)
        {
            info.Id = int.Parse(dataType.Rows[id]["ID"].ToString());
            txt_sym.Text = dataType.Rows[id]["Sym"].ToString();
            txtName.Text = dataType.Rows[id]["Name"].ToString();
            
            if (dataType.Rows[id]["Active"].ToString() == "True")
                cmbActive.Text = "Hoạt động";
            else cmbActive.Text = "Khóa";
        }

        private void lvProType_SelectedIndexChanged(object sender, EventArgs e)
        {
            ChangeButton("Update");
            ListView lv = (ListView)sender;

            if (lv.Items.Count > 0)
            {
                int flag = -1;
                for (int i = 0; i < lv.Items.Count; i++)
                {
                    if (lv.Items[i].Selected)
                    {
                        flag = i;
                    }
                }
                if (flag != -1)
                {

                    ShowListToForm(flag);

                }
            }
        }

    }
}
